import requests
from bs4 import BeautifulSoup

# def google_search(query):
#     try:
#         #https://serpapi.com/search-api
#         url = f"https://www.google.com.hk/search?q={query}&oq={query}&uule=w+CAIQICIaQXVzdGluLFRleGFzLFVuaXRlZCBTdGF0ZXM&hl=en&gl=us&sourceid=chrome&ie=UTF-8"
#         response = requests.get(url)
#         soup = BeautifulSoup(response.text, 'html.parser')
#         summaries = []
#         for li in soup.select("#search > div > div")[0].find_all('li')[:5]:
#             link_element = li.find('a')
#             href = link_element.get('href')
#             title = link_element.find('h3').text
#             abstract = ' '.join([span.text for span in li.find_all('div > div > div > div > div > div > span')])
#             summaries.append({'href': href, 'title': title, 'abstract': abstract})
#         return summaries
#     except Exception as error:
#         print("An error occurred:", error)

def bing_search(query):
    try:
        #https://serpapi.com/bing-search-api
        url = f"https://cn.bing.com/search?form=QBLH&q={query}"
        response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')
        summaries = []
        for li in soup.select("#b_results > .b_algo")[:5]:
            abstract_element = li.find('p', class_='b_lineclamp4')
            link_element = li.find('a')
            href = link_element.get('href')
            title_element = li.find('h2')
            title = title_element.text if title_element else ""
            abstract = abstract_element.text if abstract_element else ""
            abstract = abstract.replace('\u2002·\u2002', '更新。')
            summaries.append({'href': href, 'title': title, 'abstract': abstract})
        return summaries
    except Exception as error:
        print("An error occurred:", error)

# def yahoo_search(query):
#     try:
#         url = f"https://search.yahoo.com/search?p={query}&ei=UTF-8&fr=fp-tts"
#         response = requests.get(url)
#         soup = BeautifulSoup(response.text, 'html.parser')
#         summaries = []
#         for li in soup.select(".searchCenterMiddle")[0].find_all('li')[:5]:
#             comp_text_element = li.find(".compText")
#             link_element = li.find('a')
#             href = link_element.get('href')
#             title = link_element.get('aria-label')
#             abstract = comp_text_element.text if comp_text_element else ""
#             summaries.append({'href': href, 'title': title, 'abstract': abstract})
#         return summaries
#     except Exception as error:
#         print("An error occurred:", error)

# def duckduckgo_search(query):
#     try:
#         #https://serpapi.com/duckduckgo-search-api
#         # 可以改区域，这些设置的是港区
#         url = f"https://duckduckgo.com/?q={query}&kl=hk-tzh&ia=web"
#         response = requests.get(url)
#         soup = BeautifulSoup(response.text, 'html.parser')
#         summaries = []
#         for li in soup.select("#react-layout ol li")[:5]:
#             abstract_element = li.find("div:nth-child(3)").find("div")
#             link_element = li.find("div:nth-child(2)").find("a")
#             href = link_element.get("href")
#             title = link_element.text
#             abstract = abstract_element.text if abstract_element else ""
#             summaries.append({'href': href, 'title': title, 'abstract': abstract})
#         return summaries
#     except Exception as error:
#         print("An error occurred:", error)

query = "韩普,南京邮电大学管理学院,江苏省数据工程与知识服务重点实验室,邮件"
results = bing_search(query)
print(results)